import {
    getlate,
    getmylack,
    getabcense,
    getMyattendrecord,
    getMyattenddetail,
    getTypeList,
    getLateList
} from '@/services/attendance/abnormal';

export default {
    namespace: 'abnormal',
    state: {
        missData: [],
        lateData: [],
        absenceData: [],
        paginationAbsence: {
            current: 1,
            pageSize: 10,
            total: 0
        },
        paginationLate: {
            current: 1,
            pageSize: 10,
            total: 0
        },
        paginationMiss: {
            current: 1,
            pageSize: 10,
            total: 0
        },
        attendrecord: [],
        attenddetail: [],
        typeList: [],
        lackTypeList: [],
        lateTypeList: []
    },
    effects: {
        *fetchLate({ payload, callback }, { put, call }) {
            const result = yield call(getlate, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        lateData: result.data.rows,
                        paginationLate: {
                            current: result.data.pageNo,
                            pageSize: result.data.pageSize,
                            total: result.data.total
                        }
                    }
                });
            }
            if (callback) callback(result);
        },
        *fetchAbcense({ payload, callback }, { put, call }) {
            const result = yield call(getabcense, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        absenceData: result.data.rows,
                        paginationAbsence: {
                            current: result.data.pageNo,
                            pageSize: result.data.pageSize,
                            total: result.data.total
                        }
                    }
                });
            }
            if (callback) callback(result);
        },
        *fetchMylack({ payload, callback }, { put, call }) {
            const result = yield call(getmylack, payload);
            if (result && result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        missData: result.data.rows,
                        paginationMiss: {
                            current: result.data.pageNo,
                            pageSize: result.data.pageSize,
                            total: result.data.total
                        }
                    }
                });
            }
            if (callback) callback(result);
        },
        *fetchMyrecord({ payload }, { put, call }) {
            const result = yield call(getMyattendrecord, payload);
            if (result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        attendrecord: result.data.rows
                    }
                });
            }
        },
        *fetchMydetail({ payload }, { put, call }) {
            const result = yield call(getMyattenddetail, payload);
            if (result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        attenddetail: result.data.rows
                    }
                });
            }
        },
        *fetchTypeList({ payload, callback }, { put, call }) {
            const result = yield call(getTypeList, payload);
            if (result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        typeList: result.data
                    }
                });
                callback();
            }
        },
        *fetchLackTypeList({ payload, callback }, { put, call }) {
            const result = yield call(getTypeList, payload);
            if (result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        lackTypeList: result.data
                    }
                });
                callback();
            }
        },
        // 迟到早退类型列表
        *fetchLateList({ payload, callback }, { put, call }) {
            const result = yield call(getLateList, payload);
            if (result.code === 10000) {
                yield put({
                    type: 'save',
                    payload: {
                        lateTypeList: result.data
                    }
                });
                callback();
            }
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        },
        setPagination(state, { pagination }) {
            return {
                ...state,
                pagination
            };
        }
    }
};
